package com.jndv.phoneconfig;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.text.format.Formatter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jndv.phoneconfig.command.BatteryCommand;
import com.jndv.phoneconfig.command.BluetoothStateCommand;
import com.jndv.phoneconfig.command.DeviceCommand;
import com.jndv.phoneconfig.command.GpsStateCommand;
import com.jndv.phoneconfig.command.HotspotStateCommand;
import com.jndv.phoneconfig.command.MobileDataStateCommand;
import com.jndv.phoneconfig.command.MusicVolumeCommand;
import com.jndv.phoneconfig.command.PairResult;
import com.jndv.phoneconfig.fragment.BluetoothFragment;
import com.jndv.phoneconfig.fragment.DeviceInfoFragment;
import com.jndv.phoneconfig.fragment.HotspotFragment;
import com.jndv.phoneconfig.fragment.MobileDataFragment;
import com.jndv.phoneconfig.fragment.SystemFragment;
import com.jndv.phoneconfig.fragment.TimeFragment;
import com.jndv.phoneconfig.websocket.JWebSocketClient;

import org.greenrobot.eventbus.EventBus;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.URI;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    CategoryAdapter categoryAdapter;
    HotspotFragment hotspotFragment;
    BluetoothFragment bluetoothFragment;
    TimeFragment timeFragment;
    MobileDataFragment mobileDataFragment;
    SystemFragment systemFragment;
    DeviceInfoFragment deviceInfoFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView rvLeft=findViewById(R.id.rv_left);
        List<String> list=new ArrayList<>();
        list.add("热点设置");
        list.add("移动网络设置");
        list.add("时间设置");
        list.add("蓝牙设置");
        list.add("系统设置");
        list.add("设备信息");
        categoryAdapter =new CategoryAdapter(this,R.layout.item_first_category,list);
        rvLeft.setLayoutManager(new LinearLayoutManager(this));
        rvLeft.setAdapter(categoryAdapter);
        categoryAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                changeTab(position);
                categoryAdapter.setSelectPos(position);
                categoryAdapter.notifyDataSetChanged();
            }
        });
        changeTab(0);

        Button btnSocket=findViewById(R.id.btn_socket);
        btnSocket.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createWebSocket();
            }
        });

    }

    private void changeTab(int index){
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (index){
            case 0:
                if(hotspotFragment==null){
                    hotspotFragment=new HotspotFragment();
                }
                transaction.replace(R.id.fl_content,hotspotFragment).commit();
                break;
            case 1:
                if(mobileDataFragment==null){
                    mobileDataFragment=new MobileDataFragment();
                }
                transaction.replace(R.id.fl_content,mobileDataFragment).commit();
                break;
            case 2:
                if(timeFragment==null){
                    timeFragment=new TimeFragment();
                }
                transaction.replace(R.id.fl_content,timeFragment).commit();
                break;
            case 3:
                if(bluetoothFragment==null){
                    bluetoothFragment=new BluetoothFragment();
                }
                transaction.replace(R.id.fl_content,bluetoothFragment).commit();
                break;
            case 4:
                if(systemFragment==null){
                    systemFragment=new SystemFragment();
                }
                transaction.replace(R.id.fl_content,systemFragment).commit();
                break;
            case 5:
                if(deviceInfoFragment==null){
                    deviceInfoFragment=new DeviceInfoFragment();
                }
                transaction.replace(R.id.fl_content,deviceInfoFragment).commit();
                break;
        }
    }

    String serverIp;
    private JWebSocketClient client;
    //创建之前需要先判断服务端是否启动
    private void createWebSocket() {
        serverIp=PhoneUtils.getServerIP(this);
        Log.d("zhang","serverIp=="+serverIp);
        URI uri = URI.create("ws://"+serverIp+":8800");
        client = new JWebSocketClient(this,uri){

            @Override
            public void onClose(int code, String reason, boolean remote) {
                super.onClose(code, reason, remote);
                closeConnect();
            }
        };
        try {
            boolean isConnect=client.connectBlocking();
            Log.d("zhang","isConnect=="+isConnect);
            if(isConnect){
                Toast.makeText(this,"连接websocket成功",Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(this,"连接websocket失败",Toast.LENGTH_SHORT).show();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public JWebSocketClient getSocketClient(){
        return client;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        closeConnect();
    }


    /**
     * 断开连接
     */
    private void closeConnect() {
        try {
            if (null != client) {
                client.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client = null;
        }
    }


}